package com.hamster.crm.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crm.mapper.DictionaryDepartmentMapper;
import com.hamster.crm.mapper.DictionaryPositionMapper;
import com.hamster.crm.pojo.*;
import com.hamster.crm.service.EmployeeService;
import com.hamster.crm.util.ExcelUtiles;
import com.hamster.crm.util.PageResult;

import com.hamster.crm.util.XcOauth2Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 员工管理的Controller
 * 增删改查
 * @author Tarry
 * @create 2019/12/21 15:18
 */
@RestController
@RequestMapping("employee")
public class EmployeeController extends BaseController{
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DictionaryDepartmentMapper dictionaryDepartmentMapper;
    @Autowired
    private DictionaryPositionMapper dictionaryPositionMapper;
    /**
     * 新增一条员工记录
     * @param employee 员工信息
     * @return 状态码 200
     */
    @PostMapping("insert")
    public ResponseEntity<Void> insert(@RequestBody Employee employee){

        employeeService.insert(employee);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询员工信息详情
     * @param id 员工id
     * @return Employee 员工信息
     */
    @PreAuthorize("hasAuthority('component_edit')")
    @GetMapping("get")
    public ResponseEntity<Employee> get(@RequestParam String id){
        //获取当前用户信息
        XcOauth2Util xcOauth2Util = new XcOauth2Util();
        XcOauth2Util.UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
        //当前用户所属单位的id
        String name = userJwt.getName();
        String uid = userJwt.getId();
        System.out.println(uid +"    "+name);


        Employee employee= employeeService.get(id);
        return ResponseEntity.ok(employee);
    }

    /**
     * 根据员工id，修改员工信息
     * @param employee 员工信息
     * @return 状态码200
     */
    @PostMapping("update")
    public ResponseEntity<Void> update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据员工id 删除
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ResponseEntity<Void> delete(@RequestBody List<String> ids){
        employeeService.delete(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 条件查询员工列表
     * @param pageSize     每页条数
     * @param currentPage  当前页
     * @param name         姓名
     * @param startTime   入职时间开始
     * @param endTime     入职时间终止
     *
     * @param department   部门
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<PageResult<Employee>> list(
            @RequestParam(required=false) Integer pageSize,
            @RequestParam(required=false) Integer currentPage,
            @RequestParam(required = false)String name,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @RequestParam(required = false) Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @RequestParam(required = false) Date endTime,
            @RequestParam(required = false)String department
    ){
        PageResult<Employee> employeeList = employeeService.list(pageSize,currentPage,name,startTime,endTime,department);
        return ResponseEntity.ok(employeeList);
    }

    /**
     * 获取部门下拉框
     * @return
     */
    @GetMapping("getDepartmentSelect")
    public ResponseEntity<List<DictionaryDepartment>> getDepartmentSelect(){
//        QueryWrapper<DictionaryDepartment> dictionaryDepartmentQueryWrapper = new QueryWrapper<>();
//        List<DictionaryDepartment> dictionaryDepartments = dictionaryDepartmentMapper.selectList(dictionaryDepartmentQueryWrapper);
        return ResponseEntity.ok(employeeService.getDepartmentSelect());
    }

    /**
     * 获取部门下拉框【服务部、事业部】
     * @return
     */
    @GetMapping("getDepartmentSelect2")
    public ResponseEntity<List<DictionaryDepartment>> getDepartmentSelect2(){
//        QueryWrapper<DictionaryDepartment> dictionaryDepartmentQueryWrapper = new QueryWrapper<>();
//        List<DictionaryDepartment> dictionaryDepartments = dictionaryDepartmentMapper.selectList(dictionaryDepartmentQueryWrapper);
        return ResponseEntity.ok(employeeService.getDepartmentSelect2());
    }



    /**
     * 获取职位下拉框
     * @return
     */
    @GetMapping("getPositionSelect")
    public ResponseEntity<List<DictionaryPosition>> getPositionSelect(@RequestParam String departmentId){
//        QueryWrapper<DictionaryPosition> dictionaryPositionQueryWrapper = new QueryWrapper<>();
//        List<DictionaryPosition> dictionaryPositions = dictionaryPositionMapper.selectList(dictionaryPositionQueryWrapper);
        return ResponseEntity.ok(employeeService.getPositionSelect(departmentId));
    }

    /**
     * 获取上级领导的employee下拉列表
     * @param departmentId
     * @param positionId
     * @return employee 列表
     */
    @GetMapping("getSuperiorSelect")
    public ResponseEntity<List<Employee>> getSuperiorSelect(@RequestParam String departmentId,@RequestParam String positionId){
        List<Employee> list = employeeService.getSuperiorSelect(departmentId,positionId);
        return ResponseEntity.ok(list);

    }

    //TODO  excel 的倒入  暂时搁置

    /**
     * 导出全部员工数据
     * @param response
     */
    @RequestMapping("export")
    public void export(HttpServletResponse response){
        List<Employee> list =  employeeService.queryAllEmployee();
        //导出操作
        ExcelUtiles.exportExcel(list,"员工列表","员工",Employee.class,"员工信息.xls",response);
    }

    /**
     * 获取员工状态
     * @return
     */
    @GetMapping("employeeStateList")
    public ResponseEntity<List<EmployeeState>> employeeStateList(){
        List<EmployeeState> list = employeeService.employeeStateList();
        return ResponseEntity.ok(list);
    }


    /**
     * 获取招聘者 列表select下拉框
     * @return
     */
    @GetMapping("recruiterList")
    public ResponseEntity<List<Employee>> recruiterList(){
        //查询人事部员工
        List<Employee> list = employeeService.recruiterList();
        return ResponseEntity.ok(list);

    }




}
